from operator import itemgetter

def z(**kwargs):
    """
    Given an observed wavelength and ID for that wavelength, lists the
    redshift and expected positions of other galaxy
    emission/absorption lines.
    """
    qso = kwargs.pop('qso',True)

    if not qso:
        rest = dict(o2=3728.48,
                    o3a=4364.44, o3b=4960.30, o3c=5008.24,
                    h1=6564.61, h2=4862.68, h3=4341.68,
                    n2a=6549.85, n2b=6585.28,
                    s2a=6718.29, s2b=6732.67,
                    h=3968.5,k=3933.7)
                    #ca2a=8496, ca2b=8540, ca2c=8660)
    else:
        rest = dict(lya=1215.6701, n5=1240.14, si4=1400.,
                    c4=1550., c3=1908.73, mg2=2798.75,
                    h3=4341.7, hb=4862.)

    if len(kwargs) > 1:
        raise Exception('Only one input transition allowed')

    refion,obswav = kwargs.items()[0]
    if refion not in rest:
        raise Exception('Unknown ion %s' % kwargs.keys()[0])
        
    zp1 = obswav / rest[refion]

    for ion,rwa in sorted(rest.items(), key=itemgetter(1)):
        print '  %3s %5.0f:    %7.2f' % (ion, rwa, rwa*zp1)    
    print 'z=%.4f' % (zp1 - 1)
